package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseContorller;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductController extends BaseContorller {

    @Reference
    private ContractService contractService;
    @Reference
    private FactoryService factoryService;
    @Reference
    private ContractProductService contractProductService;
    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list",name = "分页查询货物列表数据")
    public String findAll(String contractId,@RequestParam(value = "page",defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        //1.用来查询生产货物的工厂
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //2,查询这个购销合同下的货物
        ContractProductExample cpExample = new ContractProductExample();
        cpExample.createCriteria().andContractIdEqualTo(contractId);
        PageInfo pageInfo = contractProductService.findAll(cpExample,pageNum,pageSize);
        request.setAttribute("page",pageInfo);
        //3.当前合同id,保存合同和货物时关联
        request.setAttribute("contractId",contractId);

        return "cargo/product/product-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转添加货物页面")
    public String toAdd(){
        return "cargo/product/product-add";
    }

    @RequestMapping(value = "/edit",name = "保存合同下货物信息")
    public String edit(ContractProduct contractProduct,MultipartFile productPhoto){
        try {
            String picPath = fileUploadUtil.upload(productPhoto);
            contractProduct.setProductImage(picPath);
        } catch (Exception e) {
            System.out.println("图片上传失败");
            e.printStackTrace();
        }

        if (StringUtils.isEmpty(contractProduct.getId())){//id为空
            contractProduct.setId(UUID.randomUUID().toString());
            contractProduct.setCompanyId(getCompanyId());
            contractProduct.setCompanyName(getCompanyName());
            contractProduct.setCreateTime(new Date());
            contractProductService.save(contractProduct);

        }else {//id不为空
            contractProductService.update(contractProduct);
        }
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractProduct.getContractId();

    }



    @RequestMapping(value = "/toUpdate",name = "进入合同下货物修改页面")
    public String update(String id){
        //根据id查找货物
        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct",contractProduct);
        //查询生产货物的厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);


        return "cargo/product/product-update";
    }

    @RequestMapping(value = "/delete",name = "删除合同下货物")
    public String delete(String id,String contractId){

        contractProductService.delete(id);

        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }


    @RequestMapping(value = "/toImport",name = "跳转到上传货物页面")
    public String toImport(String contractId){
        request.setAttribute("contractId",contractId);
        return "cargo/product/product-import";
    }
    //普通上传货物
    /*@RequestMapping(value = "/import",name = "上传货物")
    public String importXsl(String contractId,MultipartFile file) throws IOException {
        //读取上传的文件
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取工作表
        Sheet sheet = workbook.getSheetAt(0);
        int rowIndex = sheet.getLastRowNum();
        Row row = null;
        ContractProduct cp =null;
        List<ContractProduct> cpList =new ArrayList<>();
        for (int i = 1; i <= rowIndex; i++) {
            //        获取行
            cp = new ContractProduct();
            row = sheet.getRow(i);
            String factoryName = row.getCell(1).getStringCellValue();
            cp.setFactoryName(factoryName);
            String productNo = row.getCell(2).getStringCellValue();
            cp.setProductNo(productNo);
            Integer cnumber = ((Double)(row.getCell(3).getNumericCellValue())).intValue();
            cp.setCnumber(cnumber);
            String packingUnit = row.getCell(4).getStringCellValue();
            cp.setPackingUnit(packingUnit);
            String loadingRate = ((Double)row.getCell(5).getNumericCellValue()).toString();
            cp.setLoadingRate(loadingRate);
            Integer boxNum =((Double)(row.getCell(6).getNumericCellValue())).intValue();
            cp.setBoxNum(boxNum);
            Double price = (Double)(row.getCell(7).getNumericCellValue());
            cp.setPrice(price);
            String productDesc = row.getCell(8).getStringCellValue();
            cp.setProductDesc(productDesc);
            String productRequest = row.getCell(9).getStringCellValue();
            cp.setProductRequest(productRequest);

//            还需要补充几个值
            cp.setId(UUID.randomUUID().toString());
            cp.setContractId(contractId);

            cp.setCompanyId(getCompanyId());
            cp.setCompanyName(getCompanyName());
            cp.setCreateTime(new Date()); //创建时间 ,为了方便排序 新的购销合同排到前面

            cpList.add(cp);


        }
        contractProductService.saveList(cpList);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }*/

    //easyExcel上传
    @RequestMapping(value = "/import",name = "上传easy货物")
    public String importXsl(String contractId,MultipartFile file) throws IOException {
        List<ContractProduct> cpList = EasyExcel.read(file.getInputStream()).head(ContractProduct.class).sheet(0).doReadSync();
        for (ContractProduct cp : cpList) {
            cp.setId(UUID.randomUUID().toString());
            cp.setContractId(contractId);

            cp.setCompanyId(getCompanyId());
            cp.setCompanyName(getCompanyName());
            cp.setCreateTime(new Date()); //创建时间 ,为了方便排序 新的购销合同排到前面
        }

        contractProductService.saveList(cpList);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }

}
